﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DbSharp.Framework.DbObject;
using System.Collections.ObjectModel;

namespace DbSharp.Framework.CaseFile.Parser
{

    class SetupSectionExtractor
    {
        #region Private members
        private XElement m_SetupRootElement;
        private XElement m_GlobalSettingElement;
        private IEnumerable<IStatementDeclaration> m_ListStatement;
        private List<SetupAction> m_ListAction = new List<SetupAction>();
        private string m_CaseName;
        #endregion


        public SetupSectionExtractor(string caseName, XElement globalSettingElement, IEnumerable<IStatementDeclaration> listStatement, XElement setupRootElement)
        {
            m_CaseName = caseName;
            m_GlobalSettingElement = globalSettingElement;
            m_SetupRootElement = setupRootElement;
            m_ListStatement = listStatement;

            if (m_SetupRootElement == null)
                return;

            foreach (var element in m_SetupRootElement.Elements())
            {
                SetupAction setupAction = null;
                if (string.Equals(element.Name.ToString(), "statement", StringComparison.Ordinal))
                {
                    setupAction = new SetupAction();
                    setupAction.ActionType = SetupActionType.OrdinaryStatement;
                    var parser = new SetupStatementParser(m_CaseName, m_ListStatement, element);
                    setupAction.Action = parser.Statement;
                }
                else if (string.Equals(element.Name.ToString(), "dataPumpAction", StringComparison.Ordinal))
                {
                    setupAction = new SetupAction();
                    setupAction.ActionType = SetupActionType.DataPumpAction;
                    var parser = new DataPumpSectionParser(m_CaseName, m_ListStatement, m_GlobalSettingElement, element);
                    setupAction.Action = parser.PumpSection;
                }

                if (setupAction != null)
                    m_ListAction.Add(setupAction);
            }

        }

         
        #region Public property/methods

        public ReadOnlyCollection<SetupAction> ListAction
        {
            get
            {
                return new ReadOnlyCollection<SetupAction>(m_ListAction);
            }
        }

        #endregion
    }
}
